<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生日" prop="birth">
        <el-date-picker
          clearable
          v-model="queryParams.birth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生日"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="下单时间" prop="birth">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date" 
      </el-form-item> -->
      <el-form-item label="生肖" prop="zodiac">
        <el-input
          v-model="queryParams.zodiac"
          placeholder="请输入生肖"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入产品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >

        <el-button size="mini" type="primary" plain @click="look"
          >视频产出统计</el-button
        >
        <el-button
          size="mini"
          v-hasPermi="['api:videos:agent']"
          type="primary"
          plain
          @click="lookDL"
          >代理业绩统计</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      border
      v-loading="loading"
      :data="videosList"
      @selection-change="handleSelectionChange"
    >
    <!-- text -->
 
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="下单人" prop="userName" align="center"  />
      <el-table-column label="下单人（永久跟随Id）" prop="text" align="center" />
      <el-table-column label="下单人时间" prop="createTime" align="center" />
      <el-table-column label="姓名" align="center" prop="name" width="100" />
      <el-table-column label="性别" align="center" prop="sex" width="70" />
      <el-table-column label="生日" align="center" prop="birth" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生肖" align="center" prop="zodiac" width="90" />
      <el-table-column label="产品名" align="center" prop="goodsName" />
      <el-table-column label="视频/文本" align="center" prop="url" width="220">
        <template slot-scope="scope">
          <video
            v-if="scope.row.goodsType == 0"
            :src="scope.row.url"
            controls
            width="200"
            height="100"
          ></video>
          <view v-else class="">
            {{ scope.row.url }}
          </view>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="payPrice" width="80" />
      <el-table-column label="视频状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.status == 0">待上传</el-tag>
          <el-tag type="success" v-else-if="scope.row.status == 1"
            >已上传</el-tag
          >
          <el-tag type="" v-else-if="scope.row.status == 2">已下载</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="视频下载时间" align="center" prop="downloadTime">
      </el-table-column>
      <!-- downloadTime -->

      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 2">已完结</el-tag>
          <el-tag type="warning" v-else>未完结</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:lunbo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          {{ form.name }}
        </el-form-item>
        <el-form-item label="生日" prop="birth">
          {{ form.birth }}
        </el-form-item>
        <el-form-item label="生肖" prop="zodiac">
          {{ form.zodiac }}
        </el-form-item>
        <el-form-item label="产品名" prop="goodsName">
          {{ form.goodsName }}
        </el-form-item>

        <el-form-item label="视频" prop="url">
          <file-upload v-model="form.url" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openYj" append-to-body>
      <el-form
        :model="queryParams1"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        ref="queryForm1"
      >
        <el-form-item label="日期" prop="date">
          <el-date-picker
            v-model="queryParams1.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery1"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery1"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        border
        v-loading="loading"
        :data="orderList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="产品名" align="center" prop="goodsName">
          <template slot-scope="scope">
            {{ scope.row.videos.name }}
          </template>
        </el-table-column>
        <el-table-column label="总销量" align="center" prop="saleCount">
        </el-table-column>
        <el-table-column label="总销售额" align="center" prop="amount">
        </el-table-column>
        <el-table-column label="年销量" align="center" prop="yearCount">
        </el-table-column>
        <el-table-column label="年销售额" align="center" prop="yearSale">
        </el-table-column>
        <el-table-column label="月销量" align="center" prop="monthCount">
        </el-table-column>
        <el-table-column label="月销售额" align="center" prop="monthSale">
        </el-table-column>
        <el-table-column label="日销量" align="center" prop="dayCount">
        </el-table-column>

        <el-table-column label="日销售额" align="center" prop="daySale">
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="getOrderList"
      />
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openDL" append-to-body>
      <el-form
        :model="queryParams2"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        ref="queryForm1"
      >
        <el-form-item label="日期" prop="date">
          <el-date-picker
            v-model="queryParams2.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery2"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery1"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        border
        v-loading="loading"
        :data="agentList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="代理名称" align="center" prop="goodsName">
          <template slot-scope="scope">
            {{ scope.row.user.userName }}
          </template>
        </el-table-column>
        <el-table-column label="永久跟随ID" align="center" prop="goodsName">
          <template slot-scope="scope">
            {{ scope.row.user.text }}
          </template>
        </el-table-column>
        <el-table-column label="总销量" align="center" prop="saleCount">
        </el-table-column>
        <el-table-column label="总销售额" align="center" prop="amount">
        </el-table-column>
        <el-table-column label="年销量" align="center" prop="yearCount">
        </el-table-column>
        <el-table-column label="年销售额" align="center" prop="yearSale">
        </el-table-column>
        <el-table-column label="月销量" align="center" prop="monthCount">
        </el-table-column>
        <el-table-column label="月销售额" align="center" prop="monthSale">
        </el-table-column>
        <el-table-column label="日销量" align="center" prop="dayCount">
        </el-table-column>
        <el-table-column label="日销售额" align="center" prop="daySale">
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams2.pageNum"
        :limit.sync="queryParams2.pageSize"
        @pagination="getAgentSale"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listVideos,
  getVideos,
  delVideos,
  delVideos1,
  addVideos,
  updateVideos,
  sale,
  agentSale,
} from "@/api/api/videos";

export default {
  name: "Videos",
  data() {
    return {
      orderList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      openDL: false,
      // 视频表格数据
      videosList: [],
      // 弹出层标题
      title: "",
      options: [
        {
          value: "1",
          label: "已上传",
        },
        {
          value: "0",
          label: "未上传",
        },
      ],
      // 是否显示弹出层
      open: false,
      openYj: false,
      // 查询参数
      queryParams1: {
        date: "",
        year: "",
        month: "",
        day: "",
        pageNum: 1,
        pageSize: 10,
        createTime:''
      },

      queryParams2: {
        date: "",
        year: "",
        month: "",
        day: "",
        pageNum: 1,
        pageSize: 10,
      },
      agentList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sex: null,
        birth: null,
        zodiac: null,
        goodsName: null,
        url: null,
        status: null,
        userId: null,
        text1: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询视频列表 */
    getList() {
      this.loading = true;

      listVideos(this.queryParams).then((response) => {
        this.videosList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    lookDL() {
      this.openDL = true;
      this.getAgentSale();
    },
    getOrderList() {
      // 如果 queryParams1.date 有值，则格式化并传递
      if (this.queryParams1.date) {
        const formattedDate = this.formatDateString(this.queryParams1.date);
        this.$set(this.queryParams1, "year", formattedDate.year);
        this.$set(this.queryParams1, "month", formattedDate.month);
        this.$set(this.queryParams1, "day", formattedDate.day);
      } else {
        // 如果没有值，则使用当前日期
        const today = new Date();
        this.$set(this.queryParams1, "year", today.getFullYear());
        this.$set(
          this.queryParams1,
          "month",
          String(today.getMonth() + 1).padStart(2, "0")
        );
        this.$set(
          this.queryParams1,
          "day",
          String(today.getDate()).padStart(2, "0")
        );
      }
      console.log(this.queryParams1, "this.queryParams1");
      sale(this.queryParams1).then((res) => {
        this.orderList = res.rows;
      });
    },

    // agentSale

    getAgentSale() {
      console.log(12312312);
      // 如果 queryParams1.date 有值，则格式化并传递
      if (this.queryParams2.date) {
        const formattedDate = this.formatDateString(this.queryParams2.date);
        this.$set(this.queryParams2, "year", formattedDate.year);
        this.$set(this.queryParams2, "month", formattedDate.month);
        this.$set(this.queryParams2, "day", formattedDate.day);
      } else {
        // 如果没有值，则使用当前日期
        const today = new Date();
        this.$set(this.queryParams2, "year", today.getFullYear());
        this.$set(
          this.queryParams2,
          "month",
          String(today.getMonth() + 1).padStart(2, "0")
        );
        this.$set(
          this.queryParams2,
          "day",
          String(today.getDate()).padStart(2, "0")
        );
      }
      agentSale(this.queryParams2).then((res) => {
        console.log(res, "asdasdasd");
        this.agentList = res.rows;
      });
    },

    formatDateString(dateString) {
      const date = new Date(dateString);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");

      return { year, month, day };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        birth: null,
        zodiac: null,
        goodsName: null,
        url: null,
        status: null,
        userId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        text1: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleQuery2() {
      this.queryParams2.pageNum = 1;
      this.getAgentSale();
    },

    handleQuery1() {
      this.getOrderList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetQuery1() {
      this.resetForm("queryForm1");
      this.handleQuery1();
    },
    resetQuery2() {
      this.resetForm("queryForm2");
      this.handleQuery2();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVideos(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "上传视频";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.status = "1";
            updateVideos(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form.status, "asdasdasd");
            addVideos(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除视频编号为"' + ids + '"的数据项？')
        .then(function () {
          return delVideos1(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    look() {
      this.openYj = true;
      this.getOrderList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "api/videos/export",
        {
          ...this.queryParams,
        },
        `videos_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
